package com.wins.wx.api;

import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import com.wins.litemall.api.ApiFilter;
import com.wins.litemall.api.BaseLitemallApi;
import com.wins.utils.LogUtils;
import io.restassured.filter.Filter;

import static io.qameta.allure.Allure.addAttachment;
import static io.restassured.RestAssured.given;
import static org.junit.jupiter.api.Assertions.assertEquals;
// token 属于企业微信体系

public class BaseWxApi {

    public String token;

    public String getToken() {
        return token;
    }

    // 初始化token 的时候应该传入用corpId和corpSecret
    public void initToken(String corpId,  String corpSecret) {
        String res =  given()
                .queryParam("corpid",corpId)
                .queryParam("corpsecret",corpSecret)
                .log().all()
                .when()
                .get("https://qyapi.weixin.qq.com/cgi-bin/gettoken")
                .then()
                .log().all()
                .extract().response().getBody().asString();

        DocumentContext context = JsonPath.parse(res);
        Integer errcode = context.read("$.errcode");
        String errMsg = context.read("$.errmsg");
        assertEquals(errcode,0);
        assertEquals(errMsg,"ok");

        String token = context.read("$.access_token");
        this.token = token;

        addAttachment("成功获取access_token", token);

        LogUtils.logger.debug("成功获取access_token:" + token);

    }

}

